<template>
  <el-row :gutter="20">
    <el-col :span="16" :offset="4">
      <div style="margin-top: 100px; padding: 40px; border: 1px solid #f8f8f8; border-radius: 8px; box-shadow: 0 0 8px #8c939d;">
        <h2 style="text-align:center;">注册</h2>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="password2">
            <el-input v-model="ruleForm.password2" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="doClick">登陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import axios from "axios";

export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username: '',
        number: '',
        password: '',
        password2: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        number: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        password2: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let vm = this;
          if (vm.ruleForm.password !== vm.ruleForm.password2) {
            vm.$message.error('两次密码不一致');
            return false;
          }
          delete vm.ruleForm.password2;
          axios.post("/user/", vm.ruleForm).then(res => {
            vm.$message.success('注册成功，正在跳转到登陆页');
            vm.$router.push('/login');
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    doClick() {
      this.$router.push("/login");
    }
  }
}
</script>

<style scoped>

</style>